package com.fuda.trade.model

import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
import kotlinx.serialization.encodeToString
import kotlinx.serialization.json.Json

@Serializable
data class TradeOrder(
    @SerialName("id")
    val id: FudaThing?,
    @SerialName("trade_bot_id") val tradeBotId: String?=null,
    @SerialName("kline_id") val klineId: String?=null,
    @SerialName("security_id") val securityId: String?=null,
    @SerialName("strategy_id") val strategyId: String?=null,
    @SerialName("kl_volume") val klVolume: Long?=null,

    val data: TradeOrderData?,
    @SerialName("last_updated") val lastUpdated: String?=null
){

    fun serialize(): String {
      return Companion.serialize(this)
    }

    // Secondary constructor for creating a TradeOrder with only id and data
    constructor(id: FudaThing, data: TradeOrderData) :this(
        id = id,
        tradeBotId = null,
        klineId = null,
        securityId = null,
        strategyId = null,
        klVolume = null,
        data = data,
        lastUpdated = null
    )


    companion object {
        private val json = Json {
            encodeDefaults = true
            ignoreUnknownKeys=true
        }

        fun serialize(tradeOrder: TradeOrder): String {
            return json.encodeToString(tradeOrder)
        }

        fun deserialize(jsonStr: String): TradeOrder {
            return json.decodeFromString(jsonStr)
        }

        fun deserializeList(jsonStr: String): List<TradeOrder> {
            return json.decodeFromString(jsonStr)
        }
    }
}